<?php

namespace App\Http\Controllers;

use App\Models\Category;
use App\Models\Sendlist;
use App\Models\Sites;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class SendlistController extends Controller
{
    /**
     * 发文队列
     * @param [type] $word
     * @return void
     */
    public function index(Request $request)
    {
        dump(cache('sendsuccess'));
        $siteid = $request->site;
        if ($siteid) {
            $datas = Sendlist::where('sites_id', $siteid)->orderBy('is_send')->orderBy('is_out')->orderBy('publish_time')->get();
        } else {
            $datas = Sendlist::orderBy('is_send')->orderBy('is_out')->orderBy('publish_time')->get();
        }
        $sites = Sites::all();
        $cates = Category::all();
        return view('sendlist.index', compact('datas', 'sites', 'cates'));
    }

    /**
     * 添加队列数据
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'sites_id' => 'required',
            'category_id' => 'required',
            'publish_time' => 'required'
        ]);
        //得到数据
        $input = $request->except("_token");
        //转换发布时间为时间戳
        $input['publish_time'] = strtotime($input['publish_time']);
        //开启事务
        DB::beginTransaction();
        //插入数据
        $result = Sendlist::create($input);
        if ($result) {
            DB::commit();
            session()->flash('success', '添加成功');
        } else {
            DB::rollBack();
            session()->flash('error', '添加失败');
        }
        return redirect()->route('sendlist.index');
    }

    /**
     * 修改队列数据
     */
    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'sites_id' => 'required',
            'category_id' => 'required',
            'publish_time' => 'required'
        ]);
        //查出该条数据
        $sendlist = Sendlist::find($id);
        if (!$sendlist) {
            session()->flash('error', '404');
        } else {
            DB::beginTransaction();
            $save = $request->except(["_token", "id"]);
            //转换发布时间为时间戳
            $save['publish_time'] = strtotime($save['publish_time']);
            $result = $sendlist->update($save);
            if ($result) {
                DB::commit();
                session()->flash('success', '修改成功');
            } else {
                DB::rollBack();
                session()->flash('error', '修改失败');
            }
        }
        return redirect()->route('sendlist.index');
    }

    /**
     * 删除队列数据
     */
    public function destroy($id)
    {
        $model = $this->model;
        $info = $model::find($id);
        if (!$info) {
            session()->flash('error', '数据不存在');
        } else {
            $result = $info->delete();
            if ($result) {
                session()->flash('success', '删除成功');
            } else {
                session()->flash('error', '删除失败');
            }
        }
        return redirect()->route('sendlist.index');
    }
}
